package cn.jinjimi.app.presenter;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.FollowRecordChangeEvent;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.Follow;
import cn.jinjimi.app.custom.event.CustomChangeEvent;
import cn.jinjimi.app.custom.event.CustomDelEvent;
import cn.jinjimi.app.custom.event.CustomFollowRecordEvent;
import cn.jinjimi.app.custom.event.CustomGetInfoEvent;
import cn.jinjimi.app.custom.event.CustomTagGetEvent;
import cn.jinjimi.app.custom.event.InsureChangeEvent;
import cn.jinjimi.app.custom.event.ProposalChangeEvent;
import cn.jinjimi.app.custom.iview.CustomArchivsView;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.interfaces.IView;
import cn.jinjimi.app.bean.SharePrefKeys;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by libilang on 15/10/27.
 * modify by Roy on 16/06/03
 */
public class CustomArchivesPresent extends NetBasePresenter {

    private Custom mCustom;
    private CustomArchivsView mView;

    public CustomArchivesPresent(IView view, Custom custom) {
        super(view);
        mCustom = custom;
        mView = (CustomArchivsView) view;
    }

    /* 获取客户详细数据 */
    public void getCustomArchivesData(long customId) {
        CustomsManager.getInstance().getCustomInfo(customId);
    }

    /* 获取跟进记录列表 */
    private void refreshFollows() {
        CustomsManager.getInstance().getCustomFollows(mCustom.customerId);
    }

    /* 监听获取客户信息 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onfinal(final CustomGetInfoEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                if (event.data != null) {
                    mCustom = event.data;
                    mView.setCustomBaseInfo(mCustom);
                    refreshFollows();
                } else {
                    mView.disloadingDialog();
                    mView.customNull();
                }

                break;
            case DataEvent.FAIL:
                mView.showToast(R.string.custom_get_info_fail);
                mView.disloadingDialog();
                if (event.data != null) {
                    mView.setCustomBaseInfo(mCustom);
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomFollowRecordEvent(CustomFollowRecordEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                List<Follow> follows = event.data.follows;
                mView.addFootView(follows != null && follows.size() > 0);
                mView.setFollows(follows);
                break;
            case DataEvent.FAIL:
                mView.addFootView(false);
                break;
        }
        mView.disloadingDialog();
    }

    /* 监听客户资料的变化 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomChangeEvent(CustomChangeEvent event) {
        if (event.changeEvent == CustomChangeEvent.EVENT_DELETE)
            return;

        Custom custom = event.data;
        if (custom != null && custom.customerId.equals(mCustom.customerId)) {
            getCustomArchivesData(mCustom.customerId);
        }
    }

    /* 删除客户 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomDelEvent(CustomDelEvent event) {
        switch (event.state) {
            case CustomDelEvent.SUCC:

                EventBus.getDefault().post(new CustomChangeEvent(mCustom, CustomChangeEvent.EVENT_DELETE));

                mView.disloadingDialog();
                mView.showToast(R.string.delete_success);
                mView.finished();
                break;
            case DataEvent.FAIL:
                mView.disloadingDialog();
                mView.showToast(R.string.delete_fail);
                break;
        }
    }

    /* 客户保单发生更改、新增、删除、修改 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInsureChangeEvent(InsureChangeEvent event) {
        getCustomArchivesData(mCustom.customerId);
    }

    /* 计划书数量发送改变 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onProposalChangeEvent(ProposalChangeEvent event) {
        getCustomArchivesData(mCustom.customerId);
    }

    /* 监听跟进数据的变化 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFollowRecordChangeEvent(FollowRecordChangeEvent event) {
        refreshFollows();
    }

    /* 监听请求标签接口返回的数据 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomTagGetEvent(CustomTagGetEvent event) {
        switch (event.state) {
            case CustomTagGetEvent.SUCC:
                if (event.data != null && event.data.getData() != null) {
                    PreferenceManager.setData(SharePrefKeys.PREF_CUSTOM_TAGS, GsonUtils.toJson(event.data));
                    mView.setTagText();
                }
                break;
            case CustomTagGetEvent.FAIL:
                break;
        }
    }
}